
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class AesUtil{
	public static String reffer = "Referer";
	public static final String enckey = "cl6ld*3j/<.\\@0";
	static String AES = "AES";
	static String SHA1PRNG = "SHA1PRNG";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

	}

	public static String encrypt( String content )  throws GeneralSecurityException {
		return encrypt(content, enckey);
	}

	public static String decrypt( String content )  throws GeneralSecurityException {
		return decrypt(content, enckey);
	}

	/*
	 * 解密
	 */
	public static String decrypt(String sSrc, String sKey) throws GeneralSecurityException {
		try {
			// 判断Key是否正确
			if (sKey == null) {
				System.out.print("Key为空null");
				return null;
			}
			byte[] enCodeFormat = shortmd5(sKey);
			SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, "AES");

			Cipher cipher = Cipher.getInstance("AES");

			cipher.init(Cipher.DECRYPT_MODE, skeySpec);

			byte[] encrypted1 = Base64.decodeBase64( sSrc );
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original);
			return originalString;
		} catch (NoSuchAlgorithmException e){
			throw  new RuntimeException( e );
		}
	}

	/*
	 * 加密
	 */
	public static String encrypt(String sSrc, String sKey) throws GeneralSecurityException{

		if (sKey == null) {
			System.out.print("Key为空null");
			return null;
		}
		byte[] enCodeFormat;
		enCodeFormat = shortmd5(sKey);
		SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, "AES");

		Cipher cipher = Cipher.getInstance("AES");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes()); 
		return  Base64.encodeBase64URLSafeString( encrypted );
	}

	private static byte[] shortmd5(String b) {
		try{
			MessageDigest md = MessageDigest.getInstance( "MD5" );
			md.update( b.getBytes( "UTF-8" ) );
			byte[] digest = md.digest();
			return digest;
		}catch( UnsupportedEncodingException | NoSuchAlgorithmException ex ){
			throw new RuntimeException( ex );
		}
	}
	public static String encryptWithTime( Map params , String accessKey  ) throws GeneralSecurityException{
		StringBuilder sb = HttpUtil.toFormUrlEncoded( params );
		sb.insert( 0 , System.currentTimeMillis() +"," );
		return encrypt( sb.toString() , accessKey  );
	}
	public static Map<String,String> decryptMapWithTime( String params , String accessKey ) throws GeneralSecurityException{
		String queryString = AesUtil.decrypt( params , accessKey );
		int index = queryString.indexOf( "," );
		String time = queryString.substring( 0,index );
		queryString = queryString.substring( index + 1  );
		Map< String, String > map = UrlUtil.convertQueryStringToMap( queryString );
		map.put( "accessTime" , time  );
		return map;
	}


}